package com.gopay.bis.ofpay.operation.transformer;

import java.math.BigDecimal;

import com.gopay.bis.ofpay.constants.OfpayConstants;
import com.gopay.bis.ofpay.operation.transformer.doc.FlowChargeRes;
import com.gopay.bis.ofpay.operation.transformer.doc.MobiChargeRes;
import com.gopay.bis.ofpay.operation.transformer.doc.OilCardChargeRes;
import com.gopay.common.constants.bank.tpas.TpasMwaChargeConstants;
import com.gopay.common.domain.bank.tpas.mwa.FlowMessage;
import com.gopay.common.domain.bank.tpas.mwa.MobileMessage;
import com.gopay.common.domain.bank.tpas.mwa.OilCardMessage;
import com.thoughtworks.xstream.XStream;

public class TransformerCharge {
    public MobileMessage parseMobileCharge(String xml) {
        MobiChargeRes xmlBean = new MobiChargeRes();
        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true);
        xstream.processAnnotations(MobiChargeRes.class);
        xmlBean = (MobiChargeRes) xstream.fromXML(xml);
        
        MobileMessage res = new MobileMessage();
        if(OfpayConstants.CHARGE_SUCCESS.equals(xmlBean.getGame_state())
                && OfpayConstants.CHARGE_SUCCESS.equals(xmlBean.getRetcode())){
            res.setRetCode(TpasMwaChargeConstants.CHARGE_SUCCESS);
            res.setRetMsg("交易成功");
            res.setAmount(new BigDecimal(xmlBean.getOrdercash()));
            res.setGoodsName(xmlBean.getCardname());
        }else if(OfpayConstants.CHARGE_PROCESS.equals(xmlBean.getGame_state())
            && OfpayConstants.CHARGE_SUCCESS.equals(xmlBean.getRetcode())){
            res.setRetCode(TpasMwaChargeConstants.CHARGE_PROCESS);
            res.setRetMsg("第三方服务处理中");
            res.setAmount(new BigDecimal(xmlBean.getOrdercash()));
            res.setGoodsName(xmlBean.getCardname());
        }else{
            res.setRetCode(TpasMwaChargeConstants.CHARGE_FAIL);
            res.setRetMsg(xmlBean.getErr_msg());
        }
        return res;
    }

    public FlowMessage parseFlowCharge(String xml) {
        FlowChargeRes xmlBean = new FlowChargeRes();
        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true);
        xstream.processAnnotations(FlowChargeRes.class);
        xmlBean = (FlowChargeRes) xstream.fromXML(xml);
        
        FlowMessage res = new FlowMessage();
        if(OfpayConstants.CHARGE_SUCCESS.equals(xmlBean.getGame_state())
                && OfpayConstants.CHARGE_SUCCESS.equals(xmlBean.getRetcode())){
            res.setRetCode(TpasMwaChargeConstants.CHARGE_SUCCESS);
            res.setRetMsg("交易成功");
            res.setAmount(new BigDecimal(xmlBean.getOrdercash()));
            res.setGoodsName(xmlBean.getCardname());
        }else if(OfpayConstants.CHARGE_PROCESS.equals(xmlBean.getGame_state())
            && OfpayConstants.CHARGE_SUCCESS.equals(xmlBean.getRetcode())){
            res.setRetCode(TpasMwaChargeConstants.CHARGE_PROCESS);
            res.setRetMsg("第三方服务处理中");
            res.setAmount(new BigDecimal(xmlBean.getOrdercash()));
            res.setGoodsName(xmlBean.getCardname());
        }else{
            res.setRetCode(TpasMwaChargeConstants.CHARGE_FAIL);
            res.setRetMsg(xmlBean.getErr_msg());
        }
        return res;
    }

    public OilCardMessage parseOilCardCharge(String xml) {
        OilCardChargeRes xmlBean = new OilCardChargeRes();
        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true);
        xstream.processAnnotations(OilCardChargeRes.class);
        xmlBean = (OilCardChargeRes) xstream.fromXML(xml);
        
        OilCardMessage res = new OilCardMessage();
        if(OfpayConstants.CHARGE_SUCCESS.equals(xmlBean.getGame_state())
                && OfpayConstants.CHARGE_SUCCESS.equals(xmlBean.getRetcode())){
            res.setRetCode(TpasMwaChargeConstants.CHARGE_SUCCESS);
            res.setRetMsg("交易成功");
            res.setAmount(new BigDecimal(xmlBean.getOrdercash()));
            res.setGoodsName(xmlBean.getCardname());
        }else if(OfpayConstants.CHARGE_PROCESS.equals(xmlBean.getGame_state())
            && OfpayConstants.CHARGE_SUCCESS.equals(xmlBean.getRetcode())){
            res.setRetCode(TpasMwaChargeConstants.CHARGE_PROCESS);
            res.setRetMsg("第三方服务处理中");
            res.setAmount(new BigDecimal(xmlBean.getOrdercash()));
            res.setGoodsName(xmlBean.getCardname());
        }else{
            res.setRetCode(TpasMwaChargeConstants.CHARGE_FAIL);
            res.setRetMsg(xmlBean.getErr_msg());
        }
        return res;
    }
}
